package com.qianguo.shop;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.eva.baidulibrary.BDLocationClient;
import com.eva.baidulibrary.BDMapClient;
import com.eva.baidulibrary.BaiduZoomLevel;
import com.eva.baidulibrary.CustomizedOverlay;

public class MainActivity extends Activity implements View.OnClickListener {

    private Button mBtnLocation;
    private BDLocationClient mLocationClient;
    private BDMapClient mMapClient;

    private MapView mMapView;
    private BaiduMap mBaiduMap;

    PopupWindow popupWindow;
    TextView title;
    View popupView;

    private static final float DEFAULT_ZOOM_LEVEL = BaiduZoomLevel.LEVEL_200_METER;
    private static final int DEFAULT_RADIUS = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLocationClient = BDLocationClient.getInstance();
        mMapClient = BDMapClient.getInstance();
        mMapView = (MapView) findViewById(R.id.mapView);
        mBaiduMap = mMapView.getMap();
        mBtnLocation = (Button) findViewById(R.id.start_location_btn);
        mBtnLocation.setOnClickListener(this);


        popupView = LayoutInflater.from(this).inflate(R.layout.test_popup, null);
//        popupWindow = new PopupWindow(popupView);
//        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//        popupWindow.setHeight(100);
//        popupWindow.setOutsideTouchable(true);
//        popupWindow.setAnimationStyle(R.style.pulldown_in_out);
        title = (TextView) findViewById(R.id.title);
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                popupWindow.showAtLocation(title, Gravity.TOP, 0, 50);

                popupView.setVisibility(View.VISIBLE);
            }
        });




//        mBaiduMap.setMaxAndMinZoomLevel(BaiduZoomLevel.LEVEL_10_METER, BaiduZoomLevel.LEVEL_100_METER);

        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChange(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChangeFinish(MapStatus mapStatus) {
                Log.v("Eva", "zoom = " + mapStatus.zoom);
                if (mapStatus.zoom < BaiduZoomLevel.LEVEL_200_METER) {
                    mMapClient.zoom(mBaiduMap, BaiduZoomLevel.LEVEL_200_METER);
                }
            }
        });

        initMapView();
        // 开启定位图层
        mMapClient.openMyLocation(mBaiduMap, 0);
        // 定位请求
        mLocationClient.requestLocation(this, mLocationListener);
    }

    private void initMapView() {
        mBaiduMap.getUiSettings().setRotateGesturesEnabled(false);
        mBaiduMap.getUiSettings().setOverlookingGesturesEnabled(false);
        mBaiduMap.getUiSettings().setCompassEnabled(false);
    }


    /**
     * 标注覆盖物
     *
     * @param lat
     * @param lng
     */
    private void layoutMarker(double lat, double lng) {
        // 定义Marker坐标点
        LatLng point = new LatLng(lat, lng);
        // 构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.ic_map_marker);
        // 构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions().position(point).icon(bitmap);
        // 在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_location_btn:

                mLocationClient.requestLocation(this, mLocationListener);
                break;
        }
    }

    /**
     * poi搜索监听
     */
    private OnGetPoiSearchResultListener mPoiSearchListener = new OnGetPoiSearchResultListener() {
        @Override
        public void onGetPoiResult(PoiResult poiResult) {
            if (poiResult == null
                    || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
                Toast.makeText(MainActivity.this, "未找到结果", Toast.LENGTH_LONG).show();
                return;
            }

            if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
            }

        }

        @Override
        public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

        }
    };

    /**
     * 定位监听
     */
    private BDLocationListener mLocationListener = new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation != null) {
                // 显示当前位置
                mMapClient.showMyLocation(mBaiduMap, bdLocation, DEFAULT_ZOOM_LEVEL);
                search(bdLocation);
            }
            mLocationClient.stop();
        }
    };

    private void search(BDLocation location) {
        mMapClient.searchNearby(DEFAULT_RADIUS, "水果", new LatLng(location.getLatitude(), location.getLongitude()), mPoiSearchListener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapClient.closeMyLocation(mBaiduMap);
        mMapView.onDestroy();
    }

}
